<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>老师信息</title>
    <style>
        #container {
            width: 80%;
            margin: 10px auto;
        }
        .teacher {
            width: 100%;
            margin: 0 auto;
            padding: 10px 0;
            border-bottom: 1px dashed gray;
            overflow: auto;
        }
        .teacher div {
            float: left;
        }
        .photo {
            height: 140px;
            border-radius: 75px;
            overflow: hidden;
            margin-left: 20px;
        }
        .info {
            width: 75%;
            margin-left: 30px;
        }
        .info div {
            clear: both;
            margin: 5px 10px;
        }
        .info span {
            margin-right: 25px;
        }
        .info a {
            text-decoration: none;
            color: blue;
        }
        .user+h1 {
            clear: both;
        }
    </style>
</head>
<body>
    <div id="container">
        <h1 v-if="loaded">{{ subject.name }}学科的老师信息</h1>
        <hr>
        <h2 v-if="loaded && teachers.length === 0">暂无该学科老师信息</h2>
        <div class="teacher" v-for="teacher in teachers">
            <div class="photo">
                <img :src="'/static/images/' + teacher.photo" height="140" alt="">
            </div>
            <div class="info">
                <div>
                    <span><strong>姓名：{{ teacher.name }}</strong></span>
                    <span>性别：{{ teacher.sex }}</span>
                    <span>出生日期：{{ teacher.birth }}</span>
                </div>
                <div class="intro">{{ teacher.intro }}</div>
                <div class="comment">
                    <a href="" @click.prevent="praise(teacher)">好评</a>&nbsp;&nbsp;(<strong>{{ teacher.good_count }}</strong>)
                    &nbsp;&nbsp;&nbsp;&nbsp;
                    <a href="" @click.prevent="criticize(teacher)">差评</a>&nbsp;&nbsp;(<strong>{{ teacher.bad_count }}</strong>)
                </div>
            </div>
        </div>
        <div class="page">
            <a href="" @click.prevent="prevPage()">上一页</a>&nbsp;&nbsp;
            第{{ currentPage }}页 / 总{{ totalPage }}页&nbsp;&nbsp;
            <a href="" @click.prevent="nextPage()">下一页</a>
        </div>
        <p>
            <a href="/" v-if="loaded">返回首页</a>
        </p>
    </div>
    <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.11/vue.min.js"></script>
    <script>
        let app = new Vue({
            el: '#container',
            data: {
                subject: {},
                teachers: [],
                nextPageUrl: null,
                prevPageUrl: null,
                currentPage: 1,
                totalPage: 0,
                loaded: false
            },
            created() {
                let sno = location.search.substring(location.search.indexOf('=') + 1)
                fetch('/subjects/' + sno)
                    .then(resp => resp.json())
                    .then(json => this.subject = json)
                this.loadData('/teachers/?sno=' + sno)
            },
            methods: {
                loadData(url) {
                    fetch(url)
                        .then(resp => resp.json())
                        .then(json => {
                            this.teachers = json.results
                            this.prevPageUrl = json.previous
                            this.nextPageUrl = json.next
                            this.totalPage = parseInt((json.count - 1) / 5) + 1
                            this.loaded = true
                        })
                },
                prevPage() {
                    if (this.currentPage > 1) {
                        this.currentPage -= 1
                        this.loadData(this.prevPageUrl)
                    }
                },
                nextPage() {
                    if (this.currentPage < this.totalPage) {
                        this.currentPage += 1
                        this.loadData(this.nextPageUrl)
                    }
                },
                praise(teacher) {
                    fetch('/praise/?tno=' + teacher.no)
                        .then(resp => resp.json())
                        .then(json => {
                            if (json.code === 10000) {
                                teacher.good_count = json.count
                            } else {
                                alert(json.msg)
                            }
                        })
                },
                criticize(teacher) {
                    fetch('/criticize/?tno=' + teacher.no)
                        .then(resp => resp.json())
                        .then(json => {
                            if (json.code === 10000) {
                                teacher.bad_count = json.count
                            } else {
                                alert(json.msg)
                            }
                        })
                }
            }
        })
    </script>
</body>
</html>